package com.my.kq.controller;

import com.my.kq.entity.Employee;
import com.my.kq.entity.Leave;
import com.my.kq.service.EmployeeService;
import com.my.kq.service.LeaveService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/notlist.do")
public class Leave_NotListServlet extends HttpServlet {
    public LeaveService leaveService=new LeaveService();
    public EmployeeService employeeService=new EmployeeService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //查询未批准的数据
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
            String pageNo = req.getParameter("pageNo");
            if(pageNo==null || pageNo.trim().equals("")){
                pageNo="1";
            }
            //调方法分页查询已批准数据   以当前页为参数查询数据+审批状态为已审批
            String status="未批准";
            List<Leave> list=leaveService.getPage(Integer.parseInt(pageNo),status);
            //调方法查询已批准数据总数    查询总数据条数   +审批状态为已审批
            //通过lea中员工号在employee找到名字   设置进去     在Leave中额外添加了一个name属性
            for (Leave leave : list) {
                Integer employeeNumber = leave.getEmployeeNumber();
                //通过employeeNumber查找employee对象
             Employee list1=employeeService.getEmployee(employeeNumber);
             leave.setName(list1.getName());
            }
            int count=leaveService.getTotal(status);
           /* System.out.println(list);
            System.out.println(count);*/
            int totalPage=(int) Math.ceil(count/5.0);
            req.setAttribute("list",list);
            req.setAttribute("pageNo",pageNo);
            req.setAttribute("count",count);
            req.setAttribute("totalPage",totalPage);
            //跳转到noList....
            req.getRequestDispatcher("jsp/admin/leave_notlist.jsp").forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
